 /*
 * GSS - Work with Google AJAX Search API using jQuery
 * Version 0.2.0
 * @requires jQuery v1.4.2 or greater
 *
 * Examples and docs at:
 * http://gss.codeplex.com
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * @description GSS - Work with Google AJAX Search API using jQuery
 * @type jQuery
 * @name gss
 * @category Plugins/gss
 * @author Andrew S Porter / andrewporter2010@gmail.com
 */
(function($){
    jQuery.fn.GoogleWebSearch = function(opts) {    
        return this.each(function() {
        
            //extend defaults
            var o = jQuery.extend({
                execute : "",           //executes string on load
                siteRestriction : "",   //limits search scope to domain, "amazon.com"
                queryAddition : "",     //appends string to user query
                resultSetSize : "",     //how many results to return, {"small", "large"}
                linkTarget : "_blank",  //how to open links, {"_blank", "_self", "_parent"}
                drawMode : "",          //how to structure multiple searchers - more to come, {"linear", "tabbed"}
                resultsRoot : this[0],  //html element to stick results in, use document.getElementById or $()[0]
                expandMode : "open"     //how many results to show, {"closed", "open", "partial"}
            }, opts);
            
            //initialize searchControl, searchOptions, drawOptions, and webSearch objects
            _searchControl = new google.search.SearchControl();
            _searcherOptions = new google.search.SearcherOptions();
            _drawOptions = new google.search.DrawOptions();
            _webSearch = new google.search.WebSearch();
            
            //_webSearch 
            if(o.siteRestriction) _webSearch.setSiteRestriction(o.siteRestriction);
            if(o.queryAddition) _webSearch.setQueryAddition(o.queryAddition);
            if(o.resultSetSize)	
                switch(o.resultSetSize) {			
                    case "small" : _webSearch.setResultSetSize(google.search.Search.SMALL_RESULTSET); break;
                    case "large" : _webSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET); break;
                    default : $.warn("resultSetSize", o.resultSetSize);
                }
            if(o.linkTarget)
                switch (o.linkTarget) {
                    case "_blank" : _webSearch.setLinkTarget(google.search.Search.LINK_TARGET_BLANK); break;
                    case "_self" : _webSearch.setLinkTarget(google.search.Search.LINK_TARGET_SELF); break;
                    case "_top" : _webSearch.setLinkTarget(google.search.Search.LINK_TARGET_TOP); break;
                    case "_parent" : _webSearch.setLinkTarget(google.search.Search.LINK_TARGET_PARENT); break;
                    default: $.warn("linkTarget", o.linkTarget); _webSearch.setLinkTarget(o.linkTarget);
                }
             
            //_drawOptions
            if(o.drawMode)
                switch(o.drawMode) {
                    case "linear" : _drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_LINEAR); break;
                    case "tabbed" : _drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED); break;
                    default :  $.warn("drawMode", o.drawMode);
                }
                      
            //_searcherOptions
            if(o.resultsRoot) _searcherOptions.setRoot(o.resultsRoot);
            if(o.expandMode)
                switch (o.expandMode) {
                    case "closed" : _searcherOptions.setExpandMode(google.search.SearchControl.EXPAND_MODE_CLOSED); break;
                    case "open" : _searcherOptions.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN); break;
                    case "partial" : _searcherOptions.setExpandMode(google.search.SearchControl.EXPAND_MODE_PARTIAL); break;
                }
                    
            _searchControl.addSearcher(_webSearch, _searcherOptions);
            _searchControl.draw(this, _drawOptions);
            if(o.execute) _searchControl.execute(o.execute);
        });
    };

    //Used for error reporting.
    jQuery.warn = function(name, obj) {
        msg = "WARNING- Possibly invalid '" + name + "' : " + obj;
        (window.console) ? console.debug(msg) : alert(msg);
    }
})(jQuery);
